<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Validate;

class Index extends Controller
{
    public function _initialize_()
    {

        $name = session('name');
        if (!$name) {
        } else {
            $this->redirect('index/index');
        }
    }

    public function logul()
    {
        session('name', null);
        session('zhanghao', null);
    }
    public function index()
    {
        $admin = session('name');
        $this->assign('admin', $admin);
        return $this->fetch();
    }

    public function login()
    {
        if (request()->isPost()) {
            $data = [
                'name' => input('name'),
                'pwd' => input('password'),
                'code' => input('code'),
            ];
            $rule = [
                'name' => 'require',
                'pwd' => 'require',
                'code' => 'require|captcha',
            ];
            $msg = [
                'name.require' => '用户名不能为空',
                'pwd' => '密码不能为空',
                'code.require' => '验证码不能为空',
                'code.captcha' => '验证码不正确',
            ];
            $validate = new Validate($rule, $msg);
            $check = $validate->check($data);
            if (!$check) {
                $this->error($validate->getError());
            }
            $res = db('register')->where('number', $data['name'])->find();
            if (empty($res)) {
                $this->error('用户不存在');
            }
            if ($res['password'] != md5($data['pwd'])) {
                $this->error('密码错误');
            } else {
                session('zhanghao', $res['id']);
                session('name', $res['number']);
                $this->redirect('index/index/index');
            }
        }
        return $this->fetch();
    }

    public function register()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $a = [
                'number' => 'require',
                'password' => 'require',
                'password_to' => 'require|confirm:password'
            ];
            $b = [
                'number' => '账号不能为空',
                'password' => '密码不能为空',
                'password_to' => '输入密码不一致'
            ];
            $validate = new Validate($a, $b);
            $check = $validate->check($post);
            if (!$check) {
                $this->error($validate->getError());
            }
            $re = db('register')->where('number')->select();
            if (!empty($re)) {
                $this->error('账户已存在');
            }
            $date = [
                'number' => $post['number'],
                'password' => md5($post['password']),
            ];
            // dump($date);die;
            $register = db('register')->insert($date);
            if ($register) {
                $this->redirect('index/index/login');
            } else {
                $this->error('注册失败');
            }
        }
        return $this->fetch();
    }

    public function bin()
    {
        $admin = session('name');
        $this->assign('admin', $admin);

        $list = db('register')->select();
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function listt()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $admin = session('name');
            if (empty($post['imgs'])) {
                $this->error('请先选择图片');
            } else {
                $in = db('xiangmu')->insert([
                    'name' => $post['name'],
                    'danwei' => $post['danwei'],
                    'touzi' => $post['touzi'],
                    'jihua' => $post['jihua'],
                    'wancheng' => $post['wancheng'],
                    'jianshe' => $post['jianshe'],
                    'gongzuo' => $post['gongzuo'],
                    'bang' => $post['bang'],
                    'wenti' => $post['wenti'],
                    'thumb' => json_encode($post['imgs']),
                    'uname' => $admin,
                    'uid' => $post['interest']
                ]);
                if ($in) {
                    // $this->success('添加成功','index/index');
                    $arr = [
                        'code'  => 1,
                        'url' => "index/index/index"
                    ];
                    return  $arr;
                }
            }
        }
    }
    public function upload()
    {
        $file = request()->file('file');
        $info = $file->validate(['size' => 1578600, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        $arr = [];
        if ($info) {
            $arr['code'] = 1;
            $arr['msg'] = '/uploads' . DS . $info->getSaveName();
            return $arr;
        } else {
            $arr['code'] = 2;
            $arr['msg'] = "上传失败";
            return $arr;
        }
    }
    public function list()
    {
        $admin = session('name');
        $this->assign('admin', $admin);
        $list = db('xiangmu')->where(['aid' => session('id')])->select();
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function jichu()
    {
        return $this->fetch();
    }
    public function two()
    {
        return $this->fetch();
    }
    public function ji()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $in = db('jianshe')->insert([
                'xmmc' => $post['xmmc1'],
                'danwei' => $post['danwei1'],
                'shijian' => $post['shijian1'],
                'ztwcqk' => $post['ztwcqk1'],
                'bqwcqk' => $post['bqwcqk1'],
                'wancheng' => $post['wancheng1'],
                'ping' => $post['ping1']
            ]);
            if (empty($post['xmmc2'])) {
            } else {
                db('jianshe')->insert([
                    'xmmc' => $post['xmmc2'],
                    'danwei' => $post['danwei2'],
                    'shijian' => $post['shijian2'],
                    'ztwcqk' => $post['ztwcqk2'],
                    'bqwcqk' => $post['bqwcqk2'],
                    'wancheng' => $post['wancheng2'],
                    'ping' => $post['ping2']
                ]);
            };
            if (empty($post['xmmc3'])) {
            } else {
                db('jianshe')->insert([
                    'xmmc' => $post['xmmc3'],
                    'danwei' => $post['danwei3'],
                    'shijian' => $post['shijian3'],
                    'ztwcqk' => $post['ztwcqk3'],
                    'bqwcqk' => $post['bqwcqk3'],
                    'wancheng' => $post['wancheng3'],
                    'ping' => $post['ping3']
                ]);
            };
            if ($in) {
                $arr = [
                    'code'  => 1,
                    'url' => "index/index/index"
                ];
                return  $arr;
            }
        }
    }
}
